{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example: Static forward free-boundary equilibrium calculations\n", "\n", "---\n", "\n", "This example notebook shows how to use FreeGSNKE to solve **static forward** free-boundary Grad-Shafranov (GS) problems.\n", "\n", "In the **forward** solve mode we solve for the plasma equilibrium using user-defined active poloidal field coil currents, passive structure currents, and plasma current density profiles. \n", "\n", "Below, we illustrate how to use the solver for both diverted and limited plasma configurations in a **MAST-U-like tokamak** using stored pickle files containing the machine description. These machine description files partially come from the FreeGS repository and are not an exact replica of MAST-U. \n", "\n", "##### Note:\n", "It is recommended to go through the inverse solver notebook before this one as we omit many of the commonly shared details!\n", "\n", "We'll now go through the steps required to solve the **forward** problem in FreeGSNKE. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import some packages" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create the machine object\n", "\n", "First, we build the machine object from previously created pickle files in the \"machine_configs/MAST-U\" directory. \n", "\n", "FreeGSNKE requires the following paths in order to build the machine:\n", "- `active_coils_path`\n", "- `passive_coils_path`\n", "- `limiter_path`\n", "- `wall_path`\n", "- `magnetic_probe_path` (not required here)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# build machine\n", "from freegsnke import build_machine\n", "tokamak = build_machine.tokamak(\n", " active_coils_path=f\"../machine_configs/MAST-U/MAST-U_like_active_coils.pickle\",\n", " passive_coils_path=f\"../machine_configs/MAST-U/MAST-U_like_passive_coils.pickle\",\n", " limiter_path=f\"../machine_configs/MAST-U/MAST-U_like_limiter.pickle\",\n", " wall_path=f\"../machine_configs/MAST-U/MAST-U_like_wall.pickle\",\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Instantiate an equilibrium" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from freegsnke import equilibrium_update\n", "\n", "eq = equilibrium_update.Equilibrium(\n", " tokamak=tokamak, # provide tokamak object\n", " Rmin=0.1, Rmax=2.0, # radial range\n", " Zmin=-2.2, Zmax=2.2, # vertical range\n", " nx=65, # number of grid points in the radial direction (needs to be of the form (2**n + 1) with n being an integer)\n", " ny=129, # number of grid points in the vertical direction (needs to be of the form (2**n + 1) with n being an integer)\n", " # psi=plasma_psi\n", ") " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Instantiate a profile object\n", "\n", "See inverse solver notebook for a list of some of the different profile objects available in FreeGSNKE. Later on, we will try some of these out. \n", "\n", "Here, we will use the `ConstrainPaxisIp` profile." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# initialise the profiles\n", "from freegsnke.jtor_update import ConstrainPaxisIp\n", "profiles = ConstrainPaxisIp(\n", " eq=eq, # equilibrium object\n", " paxis=8e3, # profile object\n", " Ip=6e5, # plasma current\n", " fvac=0.5, # fvac = rB_{tor}\n", " alpha_m=1.8, # profile function parameter\n", " alpha_n=1.2 # profile function parameter\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load the static nonlinear solver\n", "\n", "We can now load FreeGSNKE's Grad-Shafranov static solver. The equilibrium is used to inform the solver of the computational domain and of the tokamak properties. The solver below can be used for both inverse and forward solve modes.\n", "\n", "Note: It's not necessary to instantiate a new solver when aiming to use it on new or different equilibria, as long as the integration domain, mesh grid, and tokamak are consistent across solves.\n", "\n", "The following combinations are available when choosing the `solver_type` and Grad-Shafranov operator (i.e. the elliptic operator) finite difference discretisation order:\n", "\n", "| `solver_type` | Description | Supported `gs_operator_order` | Default when `gs_operator_order=None` | Speed vs Accuracy | Notes / Recommendation |\n", "| --- | --- | --- | --- | --- | --- |\n", "| `\"LUsparse\"` (default) | Direct sparse LU factorisation of the finite-difference Grad-Shafranov operator on a uniform (R,Z) grid with Dirichlet boundary conditions. | `2`, `4` | `4` (4th-order) | Slower on large grids; highest accuracy (at order 4) | Default choice for production/high-accuracy static solves. Explicitly setting `gs_operator_order=2` works but is discouraged. |\n", "| `\"DST\"` | Frequency-based solver using a Discrete Sine Transform (DST) along Z coupled with a banded tridiagonal solve along R for Dirichlet boundary conditions. | `2` only | `2` (2nd-order) | Up to ~10× faster on large grids; 2nd-order accuracy | Preferred for exploratory runs, large grids, and evolutive simulations where a small discretisation trade-off is acceptable. Passing `gs_operator_order=4` emits a warning and is ignored. |\n", "| `\"multigrid\"` | Legacy iterative geometric multigrid solver for the finite-difference Grad-Shafranov operator. | `2`, `4` | `4` (4th-order) | Iterative multigrid | Discouraged; planned for deprecation. |\n", "\n", "A more in-depth comparison of the different solver types and performance features is provided in **example02c**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from freegsnke import GSstaticsolver\n", "GSStaticSolver = GSstaticsolver.NKGSsolver(\n", " eq,\n", " solver_type='LUsparse',\n", " gs_operator_order=4,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Define the coil currents\n", "\n", "As mentioned before, during a forward solve, we use fixed coil currents (as well as given profile functions/parameters) as inputs to solve for the equilibrium. To do this, we can use the set of currents we identified within the inverse solve notebook. Note that the passive structure currents are zero. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# load the coil currents\n", "import pickle\n", "with open('data/simple_diverted_currents_PaxisIp.pk', 'rb') as f:\n", " currents_dict = pickle.load(f)\n", " \n", "# assign currents to the eq object\n", "for key in currents_dict.keys():\n", " eq.tokamak.set_coil_current(coil_label=key, current_value=currents_dict[key])\n", " \n", "eq.tokamak.getCurrents()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The forward solve\n", "\n", "The syntax of a forward solve is identical to that of an inverse call (i.e. calling `GSStaticSolver.solve()`), however the nonlinear solver is **not** provided with a `constrain` object (i.e. we set `constrain=None`). \n", "\n", "Therefore, coil current values are not modified during the solve but instead the solver uses them as inputs to calculate the equilibrium. \n", "\n", "FreeGSNKE uses a Newton-Krylov (NK) method to solve the static forward problem stated in the inverse solve notebook (which we re-write below as a root-problem, boundary condition omitted here):\n", "\n", "$$ \\Delta^* \\psi + \\mu_0 R J_{\\phi}(\\psi,R,Z) = 0, \\qquad (R,Z) \\in \\Omega. $$\n", "\n", "Given the coil flux $\\psi_c$ is known prior to solving, we only require an initial guess for the plasma flux $\\psi_p$. This is generated automatically in FreeGSNKE and scaled automatically according to the size of the coil currents and/or plasma current. If a good initial guess is known, it can be provided to the solver in the equilibrium object above via the `psi` option. \n", "\n", "The NK method helps mitigate the numerical instability problems associated with Picard-based iterations and enables considerably more restrictive tolerance requests. The stopping criterion is defined as\n", "\n", "$$ \\frac{\\text{max} | \\psi^{(n+1)} - \\psi^{(n)} |}{\\text{max} \\ \\psi^{(n)} - \\text{min} \\ \\psi^{(n)}} < \\varepsilon, $$\n", "\n", "where $n$ is the iteration number. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# call the solver\n", "GSStaticSolver.solve(eq=eq, \n", " profiles=profiles, \n", " constrain=None, \n", " target_relative_tolerance=1e-9,\n", " verbose=True, # print output\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What we have done here is improve on the equilibrium found during the inverse solve by taking the coil currents (found during the inverse solve) and the same profile parameters and feeding them into the forward solver. \n", "\n", "We do this because it is often difficult to achieve low relative tolerances in _inverse_ solve calls (for example, the above was set at a loose target_relative_tolerance=1e-6) and so the strategy of using a forward solve after an inverse one is useful to obtain better (more converged) equilibria at stricter tolerances.\n", "\n", "As an additional example, below we manually vary some of the coil currents, perform new forward solves and compare the resulting equilibria.\n", "Note that the manual current changes cause the equilibria to transition from a diverted to a limiter configuration (this is handled through FreeGSNKE)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# copy the original eq object (for the new forward solves with modified currents)\n", "eq_forward_1 = eq.create_auxiliary_equilibrium()\n", "eq_forward_2 = eq.create_auxiliary_equilibrium()\n", "\n", "# modify the P4 current and solve\n", "eq_forward_1.tokamak.set_coil_current('P4', 1.4*eq.tokamak['P4'].current)\n", "GSStaticSolver.solve(eq=eq_forward_1, \n", " profiles=profiles, \n", " constrain=None, \n", " target_relative_tolerance=1e-9)\n", "\n", "# modify the P4 current (even more) and solve\n", "eq_forward_2.tokamak.set_coil_current('P4', 1.4**2 * eq.tokamak['P4'].current)\n", "GSStaticSolver.solve(eq=eq_forward_2, \n", " profiles=profiles, \n", " constrain=None, \n", " target_relative_tolerance=1e-9)\n", "\n", "\n", "# plot the resulting equilibria \n", "fig1, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(12, 8), dpi=80)\n", "\n", "# original \n", "ax1.grid(True, which='both')\n", "eq.plot(axis=ax1,show=False)\n", "eq.tokamak.plot(axis=ax1,show=False)\n", "ax1.set_xlim(0.1, 2.15)\n", "ax1.set_ylim(-2.25, 2.25)\n", "\n", "# modified 1\n", "ax2.grid(True, which='both')\n", "eq_forward_1.plot(axis=ax2,show=False)\n", "eq_forward_1.tokamak.plot(axis=ax2,show=False)\n", "ax2.set_xlim(0.1, 2.15)\n", "ax2.set_ylim(-2.25, 2.25)\n", "\n", "# modified 2\n", "ax3.grid(True, which='both')\n", "eq_forward_2.plot(axis=ax3,show=False)\n", "eq_forward_2.tokamak.plot(axis=ax3,show=False)\n", "ax3.set_xlim(0.1, 2.15)\n", "ax3.set_ylim(-2.25, 2.25)\n", "\n", "plt.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Alternative profile functions\n", "Here, we will illustrate how to use some of the other available profile objects.\n", "\n", "We'll start with the `ConstrainBetapIp` object." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from freegsnke.jtor_update import ConstrainBetapIp\n", "\n", "profiles_beta = ConstrainBetapIp(\n", " eq=eq,\n", " betap=0.05,\n", " Ip=6e5,\n", " fvac=0.5,\n", " alpha_m=1.8,\n", " alpha_n=1.2\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can then use it directly in a new solve (with the coil currents found by the inverse solve)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# instantiate new equilibrium object\n", "eq_beta = eq.create_auxiliary_equilibrium()\n", "\n", "# call solver with new profile object\n", "GSStaticSolver.solve(eq=eq_beta, \n", " profiles=profiles_beta, \n", " constrain=None, \n", " target_relative_tolerance=1e-9)\n", "\n", "\n", "# plot the resulting equilibria \n", "fig1, ax1 = plt.subplots(1, 1, figsize=(4, 8), dpi=80)\n", "ax1.grid(True, which='both')\n", "eq_beta.plot(axis=ax1, show=False)\n", "eq_beta.tokamak.plot(axis=ax1, show=False)\n", "ax1.set_xlim(0.1, 2.15)\n", "ax1.set_ylim(-2.25, 2.25)\n", "plt.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use the `Fiesta_Topeol` profile (see equation 13 in L. L. Lao et al (1985) Nucl. Fusion 25 1611). This has the same parameterisation as the previous two profiles except that we can now specify the `Beta0` parameter directly." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from freegsnke.jtor_update import Fiesta_Topeol\n", "\n", "profiles_topeol = Fiesta_Topeol(\n", " eq=eq, # equilibrium object\n", " Beta0=0.3665, # beta0 parameter\n", " Ip=6e5, # plasma current\n", " fvac=0.5, # fvac = rB_{tor}\n", " alpha_m=2, # profile function parameter\n", " alpha_n=1 # profile function parameter\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# instantiate new equilibrium object\n", "eq_topeol = eq.create_auxiliary_equilibrium()\n", "\n", "# call solver with new profile object\n", "GSStaticSolver.solve(eq=eq_topeol, \n", " profiles=profiles_topeol, \n", " constrain=None, \n", " target_relative_tolerance=1e-9)\n", "\n", "\n", "# plot the resulting equilibria \n", "fig1, ax1 = plt.subplots(1, 1, figsize=(4, 8), dpi=80)\n", "ax1.grid(True, which='both')\n", "eq_topeol.plot(axis=ax1, show=False)\n", "eq_topeol.tokamak.plot(axis=ax1, show=False)\n", "ax1.set_xlim(0.1, 2.15)\n", "ax1.set_ylim(-2.25, 2.25)\n", "plt.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now test the `Lao85` profile (see equations 4 and 5 in L. L. Lao et al (1985) Nucl. Fusion 25 1611).\n", "\n", "These profiles are parametrised as:\n", "\n", " $$J_{p}(\\psi, R, Z) = \\lambda\\big[ \\frac{R}{R_{0}} p'(\\tilde{\\psi}) + \\frac{R_0}{R} \\frac{1}{\\mu_0} F F'(\\tilde{\\psi}) \\big] \\quad (R,Z) \\in \\Omega_p, $$\n", "where the pressure and toroidal current profiles are given by\n", "$$ p'(\\tilde{\\psi}) = \\sum_{i=0}^{n_p} \\alpha_i \\tilde{\\psi}^i - \\hat{\\alpha} \\tilde{\\psi}^{n_p + 1} \\sum_{i=0}^{n_p} \\alpha_i$$\n", "and\n", "$$ F F'(\\tilde{\\psi}) = \\sum_{i=0}^{n_F} \\beta_i \\tilde{\\psi}^i - \\hat{\\beta} \\tilde{\\psi}^{n_F + 1} \\sum_{i=0}^{n_F} \\beta_i.$$ \n", "\n", "The required parameters are:\n", "- `Ip` (total plasma current).\n", "- `fvac` ($rB_{tor}$, vacuum toroidal field strength).\n", "- `alpha` (array of alpha coefficients).\n", "- `beta` (array of beta coefficients).\n", "- `alpha_logic`, and `beta_logic` (Booleans that correspond to $\\hat{\\alpha}$ and $\\hat{\\beta}$ above, sets boundary condition for plasma current at plasma boundary).\n", "- `Ip_logic` (if False, `Ip` is not used, if True, `Ip` is used to normalise $J_p$ and find $\\lambda$)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use the Lao85 profile to set up the same identical equilibrium as generated by the Topeol profile. \n", "\n", "In the Topeol profiles, we have $\\alpha_m = 2$ and $\\alpha_n = 1$ which means both $p'$ and $FF'$ are proportional to $1 - \\psi_n^2$ (with $\\hat{\\alpha} = \\hat{\\beta} = 1$. This corresponds to (the vectors) $\\alpha, \\beta \\propto (1, 0 , -1)$ - check this. \n", "\n", "We also need to take into account the scalings: alpha over beta is mu0." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from freegsnke.jtor_update import Lao85\n", "from freegsnke.gradshafranov import mu0 # permeability\n", "\n", "alpha = np.array([1,0,-1])\n", "beta = (1 - profiles_topeol.Beta0)/profiles_topeol.Beta0 * alpha * mu0\n", "\n", "profiles_lao = Lao85(\n", " eq=eq,\n", " Ip=6e5,\n", " fvac=0.5,\n", " alpha=alpha,\n", " beta=beta,\n", " alpha_logic=False,\n", " beta_logic=False,\n", " Ip_logic=True,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that above we're providing as input the full list of alpha and beta coefficients and therefore setting both logic inputs to False.\n", "The following is entirely equivalent to the above:\n", "\n", "```python\n", "alpha = np.array([1,0])\n", "beta = (1 - profiles_topeol.Beta0)/profiles_topeol.Beta0 * alpha * mu0\n", "\n", "profiles_lao = Lao85(\n", " eq=eq_forward,\n", " Ip=6e5,\n", " fvac=0.5,\n", " alpha=alpha,\n", " beta=beta,\n", " alpha_logic=True,\n", " beta_logic=True,\n", " Ip_logic=True,\n", ")\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# instantiate new equilibrium object\n", "eq_lao = eq.create_auxiliary_equilibrium()\n", "\n", "# call solver with new profile object\n", "GSStaticSolver.solve(eq=eq_lao, \n", " profiles=profiles_lao, \n", " constrain=None, \n", " target_relative_tolerance=1e-9)\n", "\n", "\n", "# plot the resulting equilibria \n", "fig1, ax1 = plt.subplots(1, 1, figsize=(4, 8), dpi=80)\n", "ax1.grid(True, which='both')\n", "eq_lao.plot(axis=ax1, show=False)\n", "eq_lao.tokamak.plot(axis=ax1, show=False)\n", "ax1.set_xlim(0.1, 2.15)\n", "ax1.set_ylim(-2.25, 2.25)\n", "plt.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following illustrates that the two profile functions indeed generate the same current distribution." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "laoj = profiles_lao.Jtor(R=eq.R, Z=eq.Z, psi=eq.psi())\n", "topj = profiles_topeol.Jtor(R=eq.R, Z=eq.Z, psi=eq.psi())\n", "\n", "\n", "fig1, ax1 = plt.subplots(1, 1, figsize=(5, 8), dpi=80)\n", "ax1.grid(True, which='both')\n", "plt.contourf(eq.R, eq.Z, (laoj-topj))\n", "eq.tokamak.plot(axis=ax1, show=False)\n", "plt.plot(eq.tokamak.wall.R, eq.tokamak.wall.Z, 'k', 1.2)\n", "ax1.set_xlim(0.1, 2.15)\n", "ax1.set_ylim(-2.25, 2.25)\n", "plt.tight_layout()\n", "plt.colorbar()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "FreeGSNKE also enables one to specify a set of Topeol profile parameters that best fit a set of Lao85 parameters (using the Lao_parameters method)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "alpha, beta = profiles_topeol.Lao_parameters(n_alpha=2, n_beta=2, alpha_logic=True, beta_logic=True)\n", "print(f\"Original alpha's = {profiles_lao.alpha[0:2]} vs. Fitted from Topeol = {alpha}.\")\n", "print(f\"Original beta's = {profiles_lao.beta[0:2]} vs. Fitted from Topeol = {beta}.\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "profiles_lao_fit = Lao85(\n", " eq=eq,\n", " Ip=6e5,\n", " fvac=0.5,\n", " alpha=alpha,\n", " beta=beta,\n", " alpha_logic=True,\n", " beta_logic=True,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "laoj = profiles_lao_fit.Jtor(R=eq.R, Z=eq.Z, psi=eq.psi())\n", "topj = profiles_topeol.Jtor(R=eq.R, Z=eq.Z, psi=eq.psi())\n", "\n", "\n", "fig1, ax1 = plt.subplots(1, 1, figsize=(5, 8), dpi=80)\n", "ax1.grid(True, which='both')\n", "plt.contourf(eq.R, eq.Z, (laoj-topj))\n", "eq.tokamak.plot(axis=ax1, show=False)\n", "plt.plot(eq.tokamak.wall.R, eq.tokamak.wall.Z, 'k', 1.2)\n", "ax1.set_xlim(0.1, 2.15)\n", "ax1.set_ylim(-2.25, 2.25)\n", "plt.tight_layout()\n", "plt.colorbar()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The reverse is also possible, using the Topeol_parameters method in the Lao85 profile object." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "alpha_m, alpha_n, beta_0 = profiles_lao_fit.Topeol_parameters()\n", "\n", "print(f\"Original alpha_m = {profiles_topeol.alpha_m} vs. Fitted from Lao85 = {alpha_m}.\")\n", "print(f\"Original alpha_n = {profiles_topeol.alpha_n} vs. Fitted from Lao85 = {alpha_n}.\")\n", "print(f\"Original beta_0 = {profiles_topeol.Beta0} vs. Fitted from Lao85 = {beta_0}.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Supposing an explicit parameterisation of the profiles is unknown and you have access to data describing the profiles. The `GeneralPprimeFFprime` profiles class enables you to use the raw data. \n", "\n", "As always the plasma current density is given by:\n", "\n", " $$J_{p}(\\psi, R, Z) = \\lambda\\big[ \\frac{R}{R_{0}} p'(\\tilde{\\psi}) + \\frac{R_0}{R} \\frac{1}{\\mu_0} F F'(\\tilde{\\psi}) \\big] \\quad (R,Z) \\in \\Omega_p, $$\n", "\n", "where the pressure, $p(\\tilde{\\psi})$, and toroidal magnetic field, $F(\\tilde{\\psi})$, profiles are now given by data arrays. \n", "\n", "By passing these data arrays either directly as `pprime_data` or `p_data` (and also `ffprime_data` or `f_data`), FreeGSNKE will interpolate the data and use them directly in the plasma current density above. \n", "\n", "The required parameters are:\n", "- `Ip` (total plasma current).\n", "- `fvac` ($rB_{tor}$, vacuum toroidal field strength).\n", "- `psi_n` ($\\tilde{\\psi}$, normalised poloidal flux at which data arrays corresponding, should be increasing).\n", "- `pprime_data` or `p_data` (array of $p'(\\tilde{\\psi})$ or $p(\\tilde{\\psi})$ values at the normalised poloidal flux values in `psi_n`).\n", "- `ffprime_data` or `f_data` (array of $FF'(\\tilde{\\psi})$ or $F(\\tilde{\\psi})$ values at the normalised poloidal flux values in `psi_n`).\n", "- `Ip_logic` (if False, `Ip` is not used, if True, `Ip` is used to normalise $J_p$ and find $\\lambda$).\n", "- `interpolator` (default is 'univariate_spline', otherwise can specify 'cubic_spline' - both interpolate the given data points exactly)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# instantiate new equilibrium object\n", "eq_general = eq.create_auxiliary_equilibrium()\n", "eq_general.set_plasma_psi(eq.create_psi_plasma_default())\n", "\n", "from freegsnke.jtor_update import GeneralPprimeFFprime\n", "\n", "psi_n = np.linspace(0,1,11)\n", "\n", "pprime_data = np.array([208061.72441505, 204110.95531309, 194359.61041113, 179811.09692964,\n", " 161053.35395914, 138602.79928382, 113000.64896365, 84893.04354249,\n", " 55164.04720078, 25298.53358472, 0. ])\n", "\n", "ffprime_data = np.array([0.58961295, 0.57841712, 0.5507834 , 0.50955529, 0.45639891,\n", " 0.3927777 , 0.32022539, 0.24057302, 0.1563259 , 0.07169191,\n", " 0. ])\n", "\n", "p_data = np.array([7727.96285403, 6464.13283831, 5241.18183248, 4093.46816105,\n", " 3047.85820501, 2128.71355886, 1356.94686633, 750.07340308,\n", " 320.50988963, 75.5969684 , 0. ])\n", "\n", "f_data = np.array([0.54203281, 0.53538453, 0.52887173, 0.5226858 , 0.51698575,\n", " 0.5119227 , 0.50763248, 0.50423325, 0.50181326, 0.50042828,\n", " 0.5 ])\n", "\n", "\n", "# the following three setups are equivalent\n", "profiles_general = GeneralPprimeFFprime(\n", " eq=eq_general,\n", " Ip=6e5,\n", " fvac=0.5,\n", " psi_n=psi_n,\n", " pprime_data=pprime_data,\n", " ffprime_data=ffprime_data,\n", " interpolator=\"univariate_spline\",\n", "# p_data=p_data,\n", "# f_data=f_data,\n", ")\n", "\n", "# profiles_general = GeneralPprimeFFprime(\n", "# eq=eq,\n", "# Ip=6e5,\n", "# fvac=0.5,\n", "# psi_n=psi_n,\n", "# pprime_data=pprime_data,\n", "# ffprime_data=ffprime_data,\n", "# p_data=p_data,\n", "# f_data=f_data,\n", "# )\n", "\n", "# profiles_general = GeneralPprimeFFprime(\n", "# eq=eq,\n", "# Ip=6e5,\n", "# fvac=0.5,\n", "# psi_n=psi_n,\n", "# pprime_data=None,\n", "# ffprime_data=None,\n", "# p_data=p_data,\n", "# f_data=f_data,\n", "# )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# call solver with new profile object\n", "GSStaticSolver.solve(eq=eq_general, \n", " profiles=profiles_general, \n", " constrain=None, \n", " target_relative_tolerance=1e-9)\n", "\n", "\n", "# plot the resulting equilibria \n", "fig1, ax1 = plt.subplots(1, 1, figsize=(4, 8), dpi=80)\n", "ax1.grid(True, which='both')\n", "eq_general.plot(axis=ax1, show=False)\n", "eq_general.tokamak.plot(axis=ax1, show=False)\n", "ax1.set_xlim(0.1, 2.15)\n", "ax1.set_ylim(-2.25, 2.25)\n", "plt.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Forward solve: limiter plasma\n", "\n", "Here we use the saved limiter plasma currents from the prior notebook. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# initialise the equilibrium\n", "eq_limiter = eq.create_auxiliary_equilibrium()\n", "\n", "# initialise the profiles\n", "profiles = ConstrainPaxisIp(\n", " eq=eq_limiter, # equilibrium object\n", " paxis=6e3, # profile object\n", " Ip=4e5, # plasma current\n", " fvac=0.5, # fvac = rB_{tor}\n", " alpha_m=1.8, # profile function parameter\n", " alpha_n=1.2 # profile function parameter\n", ")\n", "\n", "# load the nonlinear solver\n", "from freegsnke import GSstaticsolver\n", "GSStaticSolver = GSstaticsolver.NKGSsolver(eq_limiter) \n", "\n", "# set the coil currents\n", "import pickle\n", "with open('data/simple_limited_currents_PaxisIp.pk', 'rb') as f:\n", " current_values = pickle.load(f)\n", "\n", "for key in current_values.keys():\n", " eq_limiter.tokamak.set_coil_current(coil_label=key, current_value=current_values[key])\n", "\n", "# carry out the forward solve to find the equilibrium\n", "GSStaticSolver.solve(eq=eq_limiter, \n", " profiles=profiles, \n", " constrain=None, \n", " target_relative_tolerance=1e-9)\n", "\n", "\n", "# plot the resulting equilibria \n", "fig1, ax1 = plt.subplots(1, 1, figsize=(4, 8), dpi=80)\n", "ax1.grid(True, which='both')\n", "eq_limiter.plot(axis=ax1, show=False)\n", "eq_limiter.tokamak.plot(axis=ax1, show=False)\n", "ax1.set_xlim(0.1, 2.15)\n", "ax1.set_ylim(-2.25, 2.25)\n", "plt.tight_layout()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Forward solve: limiter plasma with no X-point covered by the grid\n", "\n", "A limited plasma can also be a valid forward equilibrium even when the solution domain does not include a useful X-point. In that case FreeGSNKE sets the plasma boundary from the limiter rather than from a diverted separatrix.\n", "\n", "The important geometric requirement is that the limiter contour lies strictly inside the rectangular solution domain, because the limited-plasma boundary is found by interpolating the flux on the limiter. The active coils and passive structures do not need to be inside this grid. The small example below uses a vertically reduced domain and an internal rectangular limiter to exercise this route.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# build a compact toy machine with active coils outside the reduced solution domain\n", "coil_dR = 0.05\n", "coil_dZ = 0.05\n", "\n", "active_coils_data_reduced_domain = {\n", " \"P1\": {\n", " \"P11\": {\n", " \"R\": [0.5], \"Z\": [0.8],\n", " \"dR\": coil_dR, \"dZ\": coil_dZ,\n", " \"resistivity\": 1e-8,\n", " \"polarity\": 1.0, \"multiplier\": 1.0,\n", " },\n", " \"P12\": {\n", " \"R\": [0.5], \"Z\": [-0.8],\n", " \"dR\": coil_dR, \"dZ\": coil_dZ,\n", " \"resistivity\": 1e-8,\n", " \"polarity\": 1.0, \"multiplier\": 1.0,\n", " },\n", " },\n", " \"P2\": {\n", " \"P21\": {\n", " \"R\": [1.6], \"Z\": [0.6],\n", " \"dR\": coil_dR, \"dZ\": coil_dZ,\n", " \"resistivity\": 1e-8,\n", " \"polarity\": 1.0, \"multiplier\": 1.0,\n", " },\n", " \"P22\": {\n", " \"R\": [1.6], \"Z\": [-0.6],\n", " \"dR\": coil_dR, \"dZ\": coil_dZ,\n", " \"resistivity\": 1e-8,\n", " \"polarity\": 1.0, \"multiplier\": 1.0,\n", " },\n", " },\n", " \"P5\": {\n", " \"R\": [1.6], \"Z\": [0.0],\n", " \"dR\": coil_dR, \"dZ\": coil_dZ,\n", " \"resistivity\": 1e-8,\n", " \"polarity\": 1.0, \"multiplier\": 1.0,\n", " },\n", "}\n", "\n", "reduced_domain_limiter_R = [0.6, 1.3, 1.3, 0.6, 0.6]\n", "reduced_domain_limiter_Z = [-0.48, -0.48, 0.48, 0.48, -0.48]\n", "reduced_domain_limiter = [\n", " {\"R\": r, \"Z\": z}\n", " for r, z in zip(reduced_domain_limiter_R, reduced_domain_limiter_Z)\n", "]\n", "\n", "tokamak_reduced_domain = build_machine.tokamak(\n", " active_coils_data=active_coils_data_reduced_domain,\n", " limiter_data=reduced_domain_limiter,\n", " wall_data=reduced_domain_limiter,\n", ")\n", "\n", "eq_reduced_domain = equilibrium_update.Equilibrium(\n", " tokamak=tokamak_reduced_domain,\n", " Rmin=0.55, Rmax=1.35,\n", " Zmin=-0.5, Zmax=0.5,\n", " nx=65,\n", " ny=65,\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# fixed coil currents and profile parameters for the forward solve\n", "reduced_domain_currents = {\n", " \"P1\": 193383.1596574935,\n", " \"P2\": -210499.54693910183,\n", " \"P5\": 69362.37774995311,\n", "}\n", "for coil_name, current in reduced_domain_currents.items():\n", " eq_reduced_domain.tokamak.set_coil_current(coil_name, current)\n", "\n", "# fix simple profile object\n", "from freegsnke.jtor_update import ConstrainBetapIp\n", "profiles_reduced_domain = ConstrainBetapIp(\n", " eq=eq_reduced_domain,\n", " betap=0.27,\n", " Ip=0.39e6,\n", " fvac=1.0,\n", " alpha_m=1.0,\n", " alpha_n=0.0,\n", ")\n", "\n", "# plasma flux guess\n", "# eq_reduced_domain.plasma_psi = (\n", "# 0.5 * np.exp(-(eq_reduced_domain.R - 0.7) ** 2 - eq_reduced_domain.Z**2) / 5\n", "# )\n", "\n", "# initialise solver and solve!\n", "GSStaticSolver_reduced_domain = GSstaticsolver.NKGSsolver(eq_reduced_domain)\n", "GSStaticSolver_reduced_domain.solve(\n", " eq=eq_reduced_domain,\n", " profiles=profiles_reduced_domain,\n", " target_relative_tolerance=1e-8,\n", " max_solving_iterations=80,\n", ")\n", "\n", "# double check everything is as expected\n", "print(f\"Limited plasma? {profiles_reduced_domain.flag_limiter}\")\n", "print(f\"Number of X-points covered by the grid: {len(profiles_reduced_domain.xpt)}\")\n", "\n", "# plot the resulting equilibrium\n", "fig1, ax1 = plt.subplots(1, 1, figsize=(5, 5), dpi=80)\n", "ax1.grid(True, which=\"both\")\n", "eq_reduced_domain.plot(axis=ax1, show=False)\n", "eq_reduced_domain.tokamak.plot(axis=ax1, show=False)\n", "plt.tight_layout()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "freegsnke", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.20" } }, "nbformat": 4, "nbformat_minor": 4 }